package edu.hawaii.myisern.brown.action;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.xml.bind.JAXBException;
import edu.hawaii.myisern.brown.model.MyIsernModel;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;

/**
 * Simple ActionBean implementation that all ActionBeans in the MyIsern class will extend.
 * 
 * @author Tim Fennell
 * @author Randy Cox
 */
/**
 * @author rcox
 *
 */
public abstract class MyIsernActionBean implements ActionBean {

  private ActionBeanContext context;

  protected MyIsernModel model = MyIsernModel.getInstance();
  private String base = System.getProperty("user.home") + "/.myisern/";

  private final String RESEARCHER_TEST_FILE = base 
    + "researchers.test.xml";
  private final String ORGANIZATION_TEST_FILE = base 
    +  "organizations.test.xml";
  private final String COLLABORATION_TEST_FILE =  base 
    + "collaborations.test.xml";

  /**
   * Set xml file location to test area.
   */
  public void setTestData() {
    model.setResearchersFile(RESEARCHER_TEST_FILE);
    model.setOrganizationsFile(ORGANIZATION_TEST_FILE);
    model.setCollaborationsFile(COLLABORATION_TEST_FILE);
    System.err.println("XML files locations set to (Test Version):");
    System.err.println(RESEARCHER_TEST_FILE);
    System.err.println(ORGANIZATION_TEST_FILE);
    System.err.println(COLLABORATION_TEST_FILE);
    loadModel();
  }

  
  /**
   * Loads data into model.
   */
  public void loadModel() {
    try {
      model.load();
    }
    catch (IOException e) {
      System.err.println("MainMenuActionBean.Java: IOException in " +
          "MyIsernActionBean.loadModel()");
      System.err.println(e.getMessage());
    }
    catch (JAXBException e) {
      System.err.println("MainMenuActionBean.Java: JAXBException in " +
          "MyIsernActionBean.loadModel()");
      System.err.println(e.getMessage());
    }
  }
  
  /**
   * Loads data into model.
   */
public void saveModel() {
  try {
    this.model.save();
  }
  catch (IOException e) {
    System.err.println("EditResearcherActionBean.save() IOException");
  }
  catch (JAXBException e) {
    System.err.println("EditResearcherActionBean.save() JaxBException");
  }
}

  /**
   * Sets context of web session.
   * 
   * @param context instance.
   */
  public void setContext(ActionBeanContext context) {
    this.context = context;
  }

  /**
   * Gets the ActionBeanContext set by Stripes during initialization.
   * 
   * @return context instance
   */
  public ActionBeanContext getContext() {
    return this.context;
  }

  /**
   * Build comma delimited string.
   * @param list list of strings
   * @return comma delimited string.
   */
  public String buildCommaStr(List<String> list) {
    String str = "";
    for (String name : list) {
      if (!str.isEmpty()) {
        str = str.concat(", ");
      }
      str = str.concat(name.trim());
    }
    return str;
  }

  /**
   * Build comma delimited string.
   * @param list list of strings
   * @return comma delimited string.
   */
  public String buildCommaStrBigInteger(List<BigInteger> list) {
    String str = "";
    for (BigInteger i : list) {
      if (!str.isEmpty()) {
        str = str.concat(", ");
      }
      str = str.concat(i.toString());
    }
    return str;
  }
  
  /**
   * Parses comma delimited string.
   * @param line string of items separated by commas
   * @return list of strings
   */
  public List<String> parseCommaStr(String line) {
    List<String> list = new ArrayList<String>();
    if (line != null) {
      StringTokenizer st = new StringTokenizer(line, ",");
      while (st.hasMoreTokens()) {
        list.add(st.nextToken());
      }
    }
    return list;
  }
  
  /**
   * Parses comma delimited string to Big Integers list.
   * @param line string of items separated by commas
   * @return list of strings
   * @throws NumberFormatException if string has non-numeric chars
   */
  public List<BigInteger> parseCommaStrBigInteger(String line) 
    throws NumberFormatException {
    List<BigInteger> list = new ArrayList<BigInteger>();
    if (line != null) {
      StringTokenizer st = new StringTokenizer(line, ",");
      while (st.hasMoreTokens()) {
        String item = st.nextToken().trim();
        try {
          Integer.parseInt(item);
        }
        catch (NumberFormatException e) {
          System.err.println("Year error: " + e.getMessage());
          throw(e);
        }
        list.add(new BigInteger(item));
      }
    }
    return list;
  }

  /**
   * Include a new item in a list.
   * 
   * @param <T> any type
   * @param item a new item
   * @param l a list of the appropriate type
   */
  protected <T> void include(T item, List<T> l) {
    if (item != null && !"".equals(item.toString())) {
      l.add(item);
    }
  }
}
